﻿using socketio.socketio;
using SocketioTest.Common;
using SocketioTest.View;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using static SocketioTest.View.UsresList;

namespace SocketioTest.ViewModel
{
    public class MainWindowViewModel : NotifyPropertyBase
    {
        public string usres_name { get; set; }
        public string avatar { get; set; }

        public Socketa Socketa { get; } = new Socketa();  //http://172.17.80.168:3000


        public object ChatContentFrame { get; set; } //聊天记录窗口
        public object UsresListFrame { get; set; } //会话窗口


        private UsersListViewModel _selectdUsersChanged { get; set; } //当前选中的对话
        public UsersListViewModel SelectdUsersChanged 
        { 
                get { return _selectdUsersChanged;}
                set
                        {
                        _selectdUsersChanged = value;
                        Notify();
                }
        }

        public UsresList usresList = new UsresList();
        public ChatContent chatContent = new ChatContent();

        public CommandBase Closewindos { get; } = new CommandBase();

        private static MainWindowViewModel? mainWindowViewModel;
        private static readonly object padlock = new object();

        public static MainWindowViewModel GetInstance()
        {
            if (MainWindowViewModel.mainWindowViewModel == null)
            {
                lock (MainWindowViewModel.padlock)
                {
                    if (MainWindowViewModel.mainWindowViewModel == null)
                        MainWindowViewModel.mainWindowViewModel = new MainWindowViewModel();
                }
            }
            return MainWindowViewModel.mainWindowViewModel;
        }

        public MainWindowViewModel()
        {

            usres_name = Global.name;
            avatar = Global.avatar;

            ChatContentFrame = chatContent;
            UsresListFrame = usresList;


            usresList.selectdUsersEvent +=new SelectdUsersDelegate(SelectdUsersChatList);

            this.Closewindos.DoExecute = new Action<object>((o) =>
            {
                (o as Window).Close();
            });
            this.Closewindos.DocanExecute = new Func<object, bool>((o) =>
            {
                return true;
            });
     
        }

        public void SelectdUsersChatList()
        {
            chatContent.MessageListDemo();
        }
    }
  

}
